/**
 * 好感度等级系统和等级系统测试插件
 * 提供指令来测试好感度等级系统和等级系统的功能
 */
import plugin from '../../../lib/plugins/plugin.js'
import favorLevel from '../core/favorLevel.js'
import levelSystem from '../core/levelSystem.js'
import api from '../core/api.js'

import menuManager from '../../systemDB/menu/menu.js'





export class FavorLevelTest extends plugin {
  constructor() {
    super({
      name: '好感度等级测试',
      dsc: '测试好感度等级系统和等级系统的功能',
      event: 'message',
      priority: 5000,
      rule: [
        {
          reg: /^#升级$/, // 好感度升级指令
          fnc: 'addExperience'
        },
        {
          reg: /^#查看经验$/, // 查看好感度经验指令
          fnc: 'viewLevelInfo'
        },
        {
          reg: /^#查看好感度经验$/, // 查看好感度经验值指令
          fnc: 'viewExperience'
        },
        {
          reg: /^#设置经验 (\d+)$/, // 设置好感度经验指令
          fnc: 'setExperience'
        },
        {
          reg: /^#等级升级$/, // 等级系统升级指令
          fnc: 'addLevelExperience'
        },
        {
          reg: /^#查看等级信息$/, // 查看等级系统信息指令
          fnc: 'viewLevelSystemInfo'
        },
        {
          reg: /^#查看等级经验$/, // 查看等级系统经验值指令
          fnc: 'viewLevelExperience'
        },
        {
          reg: /^#设置等级经验 (\d+)$/, // 设置等级系统经验指令
          fnc: 'setLevelExperience'
        }
      ]
    })
  }

  // 初始化插件
  async init() {
    // 注册到菜单
    await this.registerToMenu()
  }

  /**
     * 注册到系统菜单
     * @returns {Promise<void>}
     */
    async registerToMenu() {
      const pluginInfo = {
        name: '好感度等级测试',
        dsc: '测试好感度等级系统和等级系统的功能',
        cmds: ['#升级', '#查看经验', '#查看好感度经验', '#设置经验', '#等级升级', '#查看等级信息', '#查看等级经验', '#设置等级经验']
      }
      await menuManager.registerPluginMenu(pluginInfo)
    }

    /**
     * 等级系统升级指令 - 添加500经验
     * @param {Object} e - 消息事件对象
     * @returns {Promise<void>}
     */
    async addLevelExperience(e) {
      const userId = e.user_id
      // 使用新的配置接口，一行代码获取配置（自动处理默认值）
      const addAmount = await menuManager.getConfig('favorLevelTest', 'levelExperienceAmount', 501)
      const result = await levelSystem.addUserExperience(userId, addAmount)

      let response = `已添加${addAmount}等级经验！\n`
      response += `当前等级: ${result.newLevel}\n`
      response += `剩余经验: ${result.remainingExperience}\n`

      if (result.levelUp) {
        response += `恭喜升级！🎉`
      } else {
        const levelInfo = await levelSystem.getUserLevelInfo(userId)
        const requiredExp = levelInfo.nextLevelExperience
        response += `距离下一级还需: ${requiredExp - levelInfo.experience}经验`
      }

      await e.reply(response)
    }

    /**
     * 查看等级系统信息指令 - 查看等级和经验信息
     * @param {Object} e - 消息事件对象
     * @returns {Promise<void>}
     */
    async viewLevelSystemInfo(e) {
      const userId = e.user_id
      const levelInfo = await levelSystem.getUserLevelInfo(userId)

      let response = `等级系统信息：\n`
      response += `当前等级: ${levelInfo.level}\n`
      response += `当前经验: ${levelInfo.experience}\n`

      if (levelInfo.level < 100) {
        response += `升级所需经验: ${levelInfo.nextLevelExperience}\n`
        response += `距离下一级还需: ${levelInfo.nextLevelExperience - levelInfo.experience}经验`
      } else {
        response += `已达到满级！`
      }

      await e.reply(response)
    }

    /**
     * 查看等级系统经验值指令 - 查看当前经验值
     * @param {Object} e - 消息事件对象
     * @returns {Promise<void>}
     */
    async viewLevelExperience(e) {
      const userId = e.user_id
      const experience = await levelSystem.getUserExperience(userId)

      await e.reply(`当前等级经验: ${experience}`)
    }

    /**
     * 设置等级系统经验指令 - 设置用户经验值
     * @param {Object} e - 消息事件对象
     * @returns {Promise<void>}
     */
    async setLevelExperience(e) {
      const userId = e.user_id
      const experience = parseInt(e.match[1])

      if (isNaN(experience) || experience < 0) {
        await e.reply('请输入有效的经验值！')
        return
      }

      const success = await levelSystem.setUserExperience(userId, experience)

      if (success) {
        await e.reply(`已设置等级经验为: ${experience}`)
      } else {
        await e.reply('设置失败，请重试！')
      }
    }


  
  /**
   * 升级指令 - 添加500经验
   * @param {Object} e - 消息事件对象
   * @returns {Promise<void>}
   */
  async addExperience(e) {
    const userId = e.user_id
    // 使用新的配置接口，一行代码获取配置（自动处理默认值）
      const addAmount = await menuManager.getConfig('favorLevelTest', 'favorExperienceAmount', 10001)
    const result = await favorLevel.addUserExperience(userId, addAmount)

    let response = `已添加${addAmount}经验！\n`
    response += `当前等级: ${result.newLevel}\n`
    response += `剩余经验: ${result.remainingExperience}\n`

    if (result.levelUp) {
      response += `恭喜升级！🎉`
    } else {
      const levelInfo = await favorLevel.getUserFavorLevelInfo(userId)
      const requiredExp = levelInfo.nextLevelExperience
      response += `距离下一级还需: ${requiredExp - levelInfo.experience}经验`
    }

    await e.reply(response)
  }

  /**
   * 查看经验指令 - 查看等级和经验信息
   * @param {Object} e - 消息事件对象
   * @returns {Promise<void>}
   */
  async viewLevelInfo(e) {
    const userId = e.user_id
    const levelInfo = await favorLevel.getUserFavorLevelInfo(userId)

    let response = `好感度等级信息：\n`
    response += `当前等级: ${levelInfo.level}\n`
    response += `当前经验: ${levelInfo.experience}\n`

    if (levelInfo.level < 9) {
      response += `升级所需经验: ${levelInfo.nextLevelExperience}\n`
      response += `距离下一级还需: ${levelInfo.nextLevelExperience - levelInfo.experience}经验`
    } else {
      response += `已达到满级！`
    }

    await e.reply(response)
  }

  /**
   * 查看好感度经验指令 - 查看当前经验值
   * @param {Object} e - 消息事件对象
   * @returns {Promise<void>}
   */
  async viewExperience(e) {
    const userId = e.user_id
    const experience = await favorLevel.getUserExperience(userId)

    await e.reply(`当前好感度经验: ${experience}`)
  }

  /**
   * 设置经验指令 - 设置用户经验值
   * @param {Object} e - 消息事件对象
   * @returns {Promise<void>}
   */
  async setExperience(e) {
    const userId = e.user_id
    const experience = parseInt(e.match[1])

    if (isNaN(experience) || experience < 0) {
      await e.reply('请输入有效的经验值！')
      return
    }

    const success = await favorLevel.setUserExperience(userId, experience)

    if (success) {
      await e.reply(`已设置好感度经验为: ${experience}`)
    } else {
      await e.reply('设置失败，请重试！')
    }
  }
}